package com.example.hotelmanager.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.hotelmanager.entity.*;
import com.example.hotelmanager.entity.vo.RoomMsg;
import com.example.hotelmanager.mapper.RoomMapper;
import com.example.hotelmanager.service.*;
import com.example.hotelmanager.utils.JwtUtill;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author :chenjie
 * @date :Created 2022/11/27 22:22
 */
@Slf4j
@Service
public class RoomServiceImpl extends ServiceImpl<RoomMapper, Room> implements RoomService{
    @Autowired
    private CostTypeService costTypeService;
    @Autowired
    private BookMsgService bookMsgService;
    @Autowired
    private CheckInService checkInService;
    @Autowired
    private CostService costService;
    @Override
    public List<Room> getNullRooms() {
        Timestamp now = new Timestamp(System.currentTimeMillis());
        List<Room> rooms = this.baseMapper.selectList(null);
        //获取当前的预定信息
        List<BookMsg> bookMsgs = bookMsgService.getBookMsgByTime(now,now);
        //获取当前正在入住的信息
        List<CheckIn> checkIns = checkInService.getValidCheckIns(now,now);
        List<Room> nullRooms = new ArrayList<>();
        for(Room room:rooms){
            String roomId = room.getRoomId();
            int f = 0;
            for(BookMsg bookMsg:bookMsgs){
                if(roomId.equals(bookMsg.getResultRoom()) ){
                    f = 1;
                    break;
                }
            }
            for(CheckIn checkIn:checkIns){
                if(f == 1)
                    break;
                if(roomId.equals(checkIn.getRoomId()) ){
                    f = 1;
                    break;
                }
            }
            if(f == 0){
                nullRooms.add(room);
            }
        }
        return nullRooms;

    }

    /**
     * 新增房间的同时要在消费类型中添加房间的租金与定金
     * @param room
     */
    @Override
    public void addRoom(Room room) {

        CostType costType1;
        CostType costType2;
        String s=room.getRoomId()+"房间定金";
        String s2=room.getRoomId()+"房间租金";
        LambdaQueryWrapper<CostType> queryWrapper=new LambdaQueryWrapper<>();
        LambdaQueryWrapper<CostType> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(CostType::getName,s);
        lambdaQueryWrapper.eq(CostType::getName,s2);

        List<CostType> costTypes1 = costTypeService.list(queryWrapper);
        List<CostType> costTypes2 = costTypeService.list(lambdaQueryWrapper);
        if(costTypes1 != null && costTypes1.size() > 0 && costTypes2 != null && costTypes2.size() > 0){//已存在的话进行修改
            costType1 = costTypes1.get(0);
            costType2 = costTypes2.get(0);
            costType1.setMoney(0-room.getEarnest());
            costType1.setMoney(room.getRent());
        }else{//反之新增
            costType1 = new CostType(0,room.getRoomId()+"房间定金",0-room.getEarnest());
            costType2 = new CostType(0,room.getRoomId()+"房间租金",room.getRent());
        }
        costTypeService.saveOrUpdate(costType1);
        costTypeService.saveOrUpdate(costType2);
    }

    @Override
    public void deleteRoom(String id) {
        LambdaQueryWrapper<Cost> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(Cost::getCostTypeId,id);
        costService.remove(queryWrapper);
        LambdaQueryWrapper<BookMsg> queryWrapper2=new LambdaQueryWrapper<>();
        queryWrapper2.eq(BookMsg::getGuestIdCard,id);
        bookMsgService.remove(queryWrapper2);
        LambdaQueryWrapper<CheckIn> queryWrapper1=new LambdaQueryWrapper<>();
        queryWrapper1.eq(CheckIn::getGuestIdCard,id);
        checkInService.remove(queryWrapper1);
        LambdaQueryWrapper<CostType> queryWrapper3=new LambdaQueryWrapper<>();
        queryWrapper3.eq(CostType::getName,id+"房间定金");
        costTypeService.remove(queryWrapper3);
        LambdaQueryWrapper<CostType> queryWrapper4=new LambdaQueryWrapper<>();
        queryWrapper4.eq(CostType::getName,id+"房间租金");
        costTypeService.remove(queryWrapper4);
        this.baseMapper.deleteById(id);
    }

    @Override
    public List<Room> getNullRoomsByRank(List<Room> rooms, long fromTime, long toTime, String rank) {
        Timestamp fromTimeT = new Timestamp(fromTime);
        Timestamp toTimeT = new Timestamp(toTime);
        //获取当前的预定信息
        List<BookMsg> bookMsgs = bookMsgService.getBookMsgByTime1(fromTimeT, toTimeT);
        log.info(bookMsgs.toString());
        //获取当前正在入住的信息
        List<CheckIn> checkIns = checkInService.getValidCheckIns1(fromTimeT, toTimeT);
        List<Room> nullRooms = new ArrayList<>();
        //对比入住信息和预定信息将房间信息返回
        for (Room room : rooms) {
            if(rank.equals(room.getGrade())){
                String roomId = room.getRoomId();
                int f = 0;
                for (BookMsg bookMsg : bookMsgs) {
                    if (roomId.equals(bookMsg.getResultRoom())) {
                        f = 1;
                        break;
                    }
                }
                for (CheckIn checkIn : checkIns) {
                    if (f == 1)
                        break;
                    if (roomId.equals(checkIn.getRoomId())) {
                        f = 1;
                        break;
                    }
                }
                if (f == 0) {
                    nullRooms.add(room);
                }
            }
        }
        return nullRooms;
    }

    /**
     * 根据查询入住信息表将房间信息封装成RoomMsg向前端发送
     * @return
     */
    @Override
    public List<RoomMsg> getHasCheckedRoom() {
        Timestamp now = new Timestamp(System.currentTimeMillis());
        //获取当前正在入住的信息
        List<CheckIn> checkIns = checkInService.getValidCheckIns(now,now);
        List<RoomMsg> roomMsgs = new ArrayList<>();
        for(CheckIn checkIn:checkIns){

            Room room = this.baseMapper.selectById(checkIn.getRoomId());
            RoomMsg roomMsg = new RoomMsg(room.getRoomId(),room.getSize(),room.getGrade(),room.getRent(),room.getEarnest(),room.getMaxNum(),room.getPosition(),1,"");
            Timestamp fromTime = checkIn.getFromTime();
            Timestamp toTime = checkIn.getToTime();
            String time = fromTime.toString().substring(0,10).replace("-",".")+"-"+toTime.toString().substring(0,10).replace("-",".");
            roomMsg.setTime(time);
            roomMsgs.add(roomMsg);
        }
        return roomMsgs;
    }

    @Override
    public List<RoomMsg> getHasBookedRoom() {
        Timestamp now = new Timestamp(System.currentTimeMillis());
        List<RoomMsg> roomMsgs = new ArrayList<>();
        //获取当前的预定信息
        List<BookMsg> bookMsgs = bookMsgService.getBookMsgByTime(now,now);
        for(BookMsg bookMsg:bookMsgs){
            LambdaQueryWrapper<Room> queryWrapper=new LambdaQueryWrapper<>();
            queryWrapper.eq(Room::getRoomId,bookMsg.getResultRoom());
            Room room = this.baseMapper.selectOne(queryWrapper);
            RoomMsg roomMsg = new RoomMsg(room.getRoomId(),room.getSize(),room.getGrade(),room.getRent(),room.getEarnest(),room.getMaxNum(),room.getPosition(),0,"");
            Timestamp fromTime = bookMsg.getFromTime();
            Timestamp toTime = bookMsg.getToTime();
            String time = fromTime.toString().substring(0,10).replace("-",".")+"-"+toTime.toString().substring(0,10).replace("-",".");
            roomMsg.setTime(time);
            roomMsgs.add(roomMsg);
        }
        return roomMsgs;
    }

    @Override
    public RoomMsg getid(Room room) {
        Timestamp now = new Timestamp(System.currentTimeMillis());
        Timestamp later = new Timestamp(System.currentTimeMillis()+31536000000l);
        //获取当前的预定信息
        List<BookMsg> bookMsgs = bookMsgService.getBookMsgByTime(now,later);
        //获取当前正在入住的信息
        List<CheckIn> checkIns = checkInService.getValidCheckIns(now,now);
        RoomMsg roomMsg = new RoomMsg(room.getRoomId(),room.getSize(),room.getGrade(),room.getRent(),room.getEarnest(),room.getMaxNum(),room.getPosition(),-1,"");
        int f = 0;
        for(CheckIn checkIn:checkIns){
            if(f == 1)
                break;
            if(checkIn.getRoomId().equals(roomMsg.getRoomId())){
                roomMsg.setState(1);
                Timestamp fromTime = checkIn.getFromTime();
                Timestamp toTime = checkIn.getToTime();
                String time = fromTime.toString().substring(0,10).replace("-",".")+"-"+toTime.toString().substring(0,10).replace("-",".");
                roomMsg.setTime(time);
                f = 1;
            }
        }

        for(BookMsg bookMsg:bookMsgs){
            if(f == 1)
                break;
            if(bookMsg.getResultRoom().equals(roomMsg.getRoomId())){
                roomMsg.setState(0);
                Timestamp fromTime = bookMsg.getFromTime();
                Timestamp toTime = bookMsg.getToTime();
                String time = fromTime.toString().substring(0,10).replace("-",".")+"-"+toTime.toString().substring(0,10).replace("-",".");
                roomMsg.setTime(time);
                f = 1;
            }
        }
        return roomMsg;
    }
}
